﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.General.Entity;
using System.Web.UI.WebControls;
using System.Web.UI;
using ESF.General;
using ESF.Web.ServerControls;
using AjaxControlToolkit;

#endregion


namespace ESF.Web.Pages
{
	public class BaseCustomBrowsePagedUserControl<O, PO, VO> :
		BaseUserControl<PO>, IBaseBrowsePagedViewer<O>
		where PO : BaseBrowsePagedPresenter<O, VO>
		where VO : IBaseBrowsePagedViewer<O>
	{
		#region Propiedades

		public BrowseResults<O> Results
		{
			set
			{
				GridView grvGridView;
				DataList dtlList;
				Button btnDelete;

				DataPagging dpPager;


				dpPager = GetDataPagging();

				grvGridView = GetGridView();

				dtlList = GetDataList();


				if (DataHelper.IsEmpty(value))
				{
					OnPaggingInfoChange(PaggingInfo.Empty);
				}
				else
				{
					OnPaggingInfoChange(value.PaggingInfo);
				}

				if (grvGridView != null)
				{
					GridLoader.LoadGrid<O>(grvGridView, dpPager, value);
				}

				if (dtlList != null)
				{
					DataListLoader.LoadPagedDataList<O>(dtlList, dpPager, value);
				}


				btnDelete = GetDeleteButton();

				if (btnDelete != null)
				{
					btnDelete.Enabled = !DataHelper.IsEmpty(value);
				}
			}
		}
		
		
		public PaggingInfo frmPaggingInfo
		{
			get
			{
				PaggingInfo result;

				QueryOrderbyShowParams orderByControl;
				
				
				result = GetDataPagging ().PaggingInfo;

				orderByControl = GetQueryOrderbyShowParams ();

				if (orderByControl != null)
				{
					result.PageSize = orderByControl.PageSize;
				}

				return result;
			}
		}


		public string OrderDirection
		{
			get
			{
				QueryOrderbyShowParams orderByControl;


				orderByControl = GetQueryOrderbyShowParams();

				if (orderByControl != null)
				{
					return orderByControl.OrderDirection;
				}

				return "ASC";
			}
		}


		public string [] Orderby 
		{ 
			get
			{
				QueryOrderbyShowParams orderByControl;
				

				orderByControl = GetQueryOrderbyShowParams ();

				if (orderByControl != null)
				{
					// TODO: Soportar varios campos de ordenamiento
					return new string [] { orderByControl.OrderByField };
				}

				return null;
			} 
		}
		
		
		public bool frmIsThereResults
		{
			set 
			{
				Button btnDelete;
				
				
				btnDelete = GetDeleteButton ();
				
				if (btnDelete != null)
				{
					btnDelete.Enabled = value; 
				}
			}
		}
		
		
		public List<string> SelectedIDs
		{
			get
			{
				return GridLoader.GetSelectedIdentifiersAsString (GetGridView (), "chkSelect", "hidItemID").ToList ();
			}
		}
		
		
		public List<O> SelectedItems
		{
			get
			{
				return GetSelectedItems(SelectedIDs);
			}
		}
		
		#endregion
		
		
		#region Funciones
		
		public void SetHiddenFieldForID (GridViewRow pGridViewRow, int pID)
		{
			GridLoader.SetHiddenFieldForID(pGridViewRow, pID);
		}
		
		
		public void SetHiddenFieldForID (GridViewRow pGridViewRow, string pID)
		{
			GridLoader.SetHiddenFieldForID (pGridViewRow, pID);
		}
		
		
		public virtual List<O> GetSelectedItems (List<string> pIDs) { return null; }
		
		
		protected void Page_Init (object sender, EventArgs e)
		{
			DataPagging dpPager;
			
			
			dpPager = GetDataPagging ();
			
			if (dpPager != null)
			{
				dpPager.Visible = false;
			}
		}
		
		
		protected virtual DataList GetDataList () { return null; }
		
		protected virtual GridView GetGridView () { return null; }
		
		protected virtual DataPagging GetDataPagging () { return null; }

		protected virtual QueryOrderbyShowParams GetQueryOrderbyShowParams () { return null; }
		
		protected virtual Button GetDeleteButton () { return null; }
		

		protected void Browse(bool pFromButton = false)
		{
			Trace.Write("BaseCustomBrowsePagedUserControl Start");

			_Presenter.Browse (pFromButton);

			Trace.Write("BaseCustomBrowsePagedUserControl End");
		}
		
		
		protected void Clear ()
		{
			GridView grvView;
			DataList dtlList;
			
			
			grvView = GetGridView ();
			
			if (grvView != null) { grvView.Visible = false; }
		
			dtlList = GetDataList ();
			
			if (dtlList != null) { dtlList.Visible = false; }
			
			_Presenter.Clear ();

			GetDataPagging ().Visible = false;

			OnClear ();
		}
		
		
		protected void Delete ()
		{
			_Presenter.Delete ();
		}
		
		#endregion
		
		
		#region Eventos de los Botones

		public virtual void OnPaggingInfoChange(PaggingInfo pPagInfo) {}


		protected virtual void btnBrowse_Click(object sender, EventArgs e)
		{
			GetDataPagging ().Reset ();

			Browse (true);
		}
		
		
		protected virtual void btnClear_Click(object sender, EventArgs e)
		{
			Clear ();
		}


		protected virtual void btnDelete_Click(object sender, EventArgs e)
		{
			Delete ();
		}


		public virtual void AfterBrowse(O pFilter, PaggingInfo pPagInfo)
		{

		}


		public virtual void AfterDoBrowse(O pFilter, PaggingInfo pPagInfo)
		{

		}
		
		#endregion
		
		
		#region Manejo de Eventos
		
		protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
		{
			GridLoader.CheckHeaderAll (GetGridView (), "chkSelectAll", "chkSelect");
		}
		
		
		protected void dpggrvResults_OnPageChanged()
		{
			_Presenter.Browse();
		}
		
		#endregion
	}
}
